package com.efruit.struts.action;

import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.efruit.externalInterfaces.IShopCart;
import com.efruit.struts.form.SelectForm;
import com.efruit.to.ShopCartTO;

public class SelectAction extends Action {
	private IShopCart shopcart = null;

	public IShopCart getShopcart() {
		return shopcart;
	}

	public void setShopcart(IShopCart shopcart) {
		this.shopcart = shopcart;
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		if (request.getParameter("select") != null) {
			SelectForm selectForm = (SelectForm) form;
			System.out.println("selectForm:" + selectForm.getId());
			int amount = shopcart.addProduct(selectForm.getId());
			if (amount == 0)
				return mapping.findForward("error");
			Vector<ShopCartTO> infos = shopcart.getShoppingInfo();
			request.setAttribute("shopcartInfos", infos);
			return mapping.findForward("shopcart");
		}
		else{
			System.out.println("select null");
		}
		return null;
	}
}
